package io.renren.service.impl;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import io.renren.controller.AbstractController;
import io.renren.dao.EvaluationInformationDao;

import io.renren.entity.EvaluationInformationEntity;
import io.renren.entity.SysUserEntity;
import io.renren.utils.ShiroUtils;
import io.renren.validator.ValidatorUtils;
import io.renren.validator.group.AddGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.dao.EmployeePerformanceManagementDao;
import io.renren.entity.EmployeePerformanceManagementEntity;
import io.renren.service.EmployeePerformanceManagementService;
import org.springframework.transaction.annotation.Transactional;


@Service("employeePerformanceManagementService")
public class EmployeePerformanceManagementServiceImpl  extends  AbstractController implements EmployeePerformanceManagementService {
	@Autowired
	private EmployeePerformanceManagementDao employeePerformanceManagementDao;

	@Autowired
	private EvaluationInformationDao evaluationInformationDao;
	
	@Override
	public EmployeePerformanceManagementEntity queryObject(Integer id){
		return employeePerformanceManagementDao.queryObject(id);
	}
	
	@Override
	public List<EmployeePerformanceManagementEntity> queryList(Map<String, Object> map){
		return employeePerformanceManagementDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return employeePerformanceManagementDao.queryTotal(map);
	}
	
	@Override
	public void save(EmployeePerformanceManagementEntity employeePerformanceManagement){
		employeePerformanceManagementDao.save(employeePerformanceManagement);
	}

	@Override
	public void update(JSONObject params){
		EmployeePerformanceManagementEntity pojo = JSON.parseObject(params.get("employeePerformanceManagement").toString(), EmployeePerformanceManagementEntity.class);
		List<EvaluationInformationEntity> evaluationInformationList = JSON.parseArray(params.get("evaluationInformationList").toString(), EvaluationInformationEntity.class);
		String self = params.get("self").toString();

		BigDecimal baseSalary=pojo.getBaseSalary();
		/*if(baseSalary!=null){
			pojo.setIslocked("0");
		}*/


		employeePerformanceManagementDao.update(pojo);
        //考评信息有多条，可能既有更新删除增加操作，所以先删在新增,要记下考评的创建人和创建时间
		evaluationInformationDao.deleteByAchievementsId(pojo.getId());
		saveKpxx(evaluationInformationList,pojo,self);
	}
	
	@Override
	public void delete(Integer id){
		employeePerformanceManagementDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		employeePerformanceManagementDao.deleteBatch(ids);
		for( int id:ids){
			evaluationInformationDao.deleteByAchievementsId(id);//删除关联的考评信息

		}
	}

	@Override
	public List<EvaluationInformationEntity> selectKPjl(String  id) {

		return  evaluationInformationDao.selectKPjl(new Integer(id));//通过员工绩效表id关联考评表绩效id查询

	}

	@Override
	@Transactional
	public void saveAndUpdateKpxx(JSONObject params){
		//EmployeePerformanceManagementEntity employeePerformanceManagement =(EmployeePerformanceManagementEntity)params.get("employeePerformanceManagement");

		EmployeePerformanceManagementEntity employeePerformanceManagement = JSON.parseObject(params.get("employeePerformanceManagement").toString(), EmployeePerformanceManagementEntity.class);

		SysUserEntity entity= (SysUserEntity) ShiroUtils.getSubject().getSession().getAttribute("user");
		employeePerformanceManagement.setEmpName(entity.getUsername());
		employeePerformanceManagement.setEmpId(entity.getUserId().toString());
		employeePerformanceManagement.setCompany(entity.getCompany());
		employeePerformanceManagement.setCompanyId(entity.getCompanyId());
		employeePerformanceManagement.setDept(entity.getDept());
		employeePerformanceManagement.setCreateDeptId(entity.getDeptNo());
		employeePerformanceManagement.setPost(entity.getPost());
		employeePerformanceManagement.setCusername(entity.getUsername());
		employeePerformanceManagement.setCdate(new Date());



		List<EvaluationInformationEntity> evaluationInformationList = JSON.parseArray(params.get("evaluationInformationList").toString(), EvaluationInformationEntity.class);

	//	List<EvaluationInformationEntity> evaluationInformationList =(List<EvaluationInformationEntity>)params.get("evaluationInformationList");
		ValidatorUtils.validateEntity(employeePerformanceManagement, AddGroup.class);
		employeePerformanceManagementDao.save(employeePerformanceManagement);

		   saveKpxx(evaluationInformationList,employeePerformanceManagement,"Y");//保存考评信息

	}


	@Override
	public Map info(Integer id){
		EmployeePerformanceManagementEntity employeePerformanceManagement = employeePerformanceManagementDao.queryObject(id);
		List<EvaluationInformationEntity> evaluationInformationList =evaluationInformationDao.selectKPjl(employeePerformanceManagement.getId());
		Map map=new HashMap();
		//查询出考评信息和员工绩效信息
		map.put("employeePerformanceManagement", employeePerformanceManagement);
		map.put("evaluationInformationList", evaluationInformationList);
		return map;
	}
	@Transactional
    void saveKpxx(List<EvaluationInformationEntity>  list,EmployeePerformanceManagementEntity employeePerformanceManagement,String self){
		for (EvaluationInformationEntity pojo : list) {
			pojo.setAchievementsId(employeePerformanceManagement.getId());
			pojo.setEmpName(employeePerformanceManagement.getEmpName());
			pojo.setEmpId(employeePerformanceManagement.getEmpId());
			pojo.setDepartment(employeePerformanceManagement.getDept());
			pojo.setPost(employeePerformanceManagement.getPost());
			pojo.setPostId(employeePerformanceManagement.getPostId());
			pojo.setYear(employeePerformanceManagement.getYear());
			pojo.setMouth(employeePerformanceManagement.getMouth());
			pojo.setCusername(employeePerformanceManagement.getEmpName());
			pojo.setCdate(employeePerformanceManagement.getCdate());
			//如果为N则为领导评分，需要记载领导和时间
			if("N".equals(self)){
				pojo.setApprover(getUser().getUsername());
				pojo.setApproveDate(new Date());
			}
			ValidatorUtils.validateEntity(pojo, AddGroup.class);
			evaluationInformationDao.save(pojo);
		}
	}


	@Override
	public void lock(Integer[] ids){
		for( int id:ids){
			employeePerformanceManagementDao.lock(id);

		}
	}


}
